/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：SavingScreen.java <br>
 * 创建时间：2013-9-6 下午6:14:21 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;

import lostland.gmud.exv2.blgframework.impl.BLGFileIO;
import lostland.gmud.exv2.blgframework.impl.BLGGraphics;
import lostland.gmud.exv2.data.SaveData;
import lostland.gmud.exv2.expand.DesUtils;
import lostland.gmud.exv2.ui.core.FullScreen;
import lostland.gmud.exv2.ui.core.GmudWindow;
import lostland.gmud.exv2.ui.core.NewButton;

/**
 * 类名：SavingScreen <br>
 * 说明：存档
 *
 * @author 12548
 */
@SuppressLint("NewApi")
public class SavingScreen extends FullScreen {

	boolean b = true;

	String s = "保存中";


	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.DialogScreen#onTouchDown(int, int)
	 */
	@Override
	protected void onTouchDown(int tx, int ty) {
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.DialogScreen#onTouchMove(int, int)
	 */
	@Override
	protected void onTouchMove(int tx, int ty) {
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.DialogScreen#onTouchUp(int, int)
	 */
	@Override
	protected void onTouchUp(int tx, int ty) {

	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.DialogScreen#onClick(int, int)
	 */
	@Override
	protected void onClick(int tx, int ty) {
		getGame().popToRootScreen();
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.CScreen#present(float)
	 */
	@Override
	public void present(float deltaTime) {
		new GmudWindow(50, 50, 60, 14) {

			@Override
			public void draw() {
				drawBackground();
			}

		}.draw();
		BLGGraphics g = (BLGGraphics) getGame().getGraphics();
		g.drawText(s, 51, 51, FontSize.FT_12PX);
	}


	/**
	 * @see lostland.gmud.exv2.LoadingScreen.loadPerf()
	 */
	public static void savePerf() {
		BLGFileIO f = (BLGFileIO) Game.getInstance().getFileIO();
		SharedPreferences sp = f.getPreferences();
		Editor edd = sp.edit();

		edd.putInt("SaveFileVersion", GameConstants.SAVEGAME_VER);
		edd.putBoolean("newgame", false);
		edd.putBoolean("标准键位", Game.classickeymap);
		edd.putInt("图块", Gmud.tileset);
		edd.putBoolean("显示菜单键", NewButton.menubutton);
		edd.putBoolean("返回键退出", Game.backkeyquit);
		edd.putBoolean("声音开关", Game.soundEnabled);
		edd.putInt("按键颜色", NewButton.color);
		edd.putBoolean("不间断学习", Game.uninterruptible);
		edd.putInt("地图分辨率", Game.resolution_level);
		edd.putBoolean("自动检查更新", Game.autocheckupdate);
		edd.putBoolean("只在wifi下更新", Game.updateonlyonwifi);
		edd.putInt("背景音乐等级", Game.bgm_level);
		edd.putBoolean("自动添加轻功点", Game.autoAddFlyPoint);
		edd.putBoolean("菜单键使用轻功", Game.menuKeyForFly);

		if (!edd.commit()) {
			savePerf();
		}
	}

	public boolean save() {
//		if(!GmudWorld.mc.name.contains("凸凸") && GmudWorld.mc.skills[Skill.WRENCH_INDEX] > 0){
//			GmudWorld.mc.skills[Skill.WRENCH_INDEX] = 0;
//			GmudWorld.mc.drop(Item.WRENCH_INDEX, -1);
//		}

		savePerf();

		Logger.i("0");

		BLGFileIO f = (BLGFileIO) getGame().getFileIO();

		Logger.i("1");

		SharedPreferences sp = f.getPreferences();
		Editor edd = sp.edit();

		Logger.i("2");


		Gson gson = new Gson();

		Logger.i("3");


		String nsf = gson.toJson(SaveData.getInstance());

		Logger.json(nsf);

		try {
			edd.putString("save", new DesUtils().encrypt(nsf));
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

//		String mcc = gson.toJson(GmudWorld.mc);
//
//		GmudGame.logInfo("Saving gsonmc", mcc);
//
//		String gm = gson.toJson(GmudGame.getInstance().data);
//
//		GmudGame.logInfo("Saving gm", gm);

//		edd.putString("save", sv);
//		try {
//			edd.putString("mainchar", SimpleCrypto.encrypt(GameConstants.password, mcc));
//			edd.putString("instance", SimpleCrypto.encrypt(GameConstants.password, gm));

//			edd.putString("mainchar",mcc);
//			edd.putString("instance",gm);
//
//			for(int i = 0; i < game.oldmc.size(); i++)
//			{
//				edd.putString("oldmc" + i, SimpleCrypto.encrypt(GameConstants.password, gson.toJson(instance.oldmc.get(i))));
//				edd.putString("oldmc" + i, gson.toJson(game.oldmc.get(i)));
//			}
//
//		} catch (Exception e) {
//			e.printStackTrace();
//			return false;
//		}


		while (!edd.commit()) {
		}

		return true;
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.DialogScreen#update(float)
	 */
	@Override
	public void update(float deltaTime) {
		if (b) {
			b = false;
			s = save() ? "存档成功！" : "存档失败！";
//			Gmud.mc.setMaxhp(Gmud.mc.getMaxhpInTheory());
		}
		super.update(deltaTime);
	}
}
